package com.choichumgming.rentcat.web.controller;


import com.choichumgming.rentcat.common.api.CommonResult;
import com.choichumgming.rentcat.dto.MemberDetails;
import com.choichumgming.rentcat.dto.UmsLoginOrRegisterParam;
import com.choichumgming.rentcat.model.UmsMember;
import com.choichumgming.rentcat.service.UmsMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 会员登录注册管理Controller
 */
@RestController
@Api(tags = "UmsMemberController", description = "会员登录注册管理")
@RequestMapping("/")
public class UmsMemberController {

    @Autowired
    private UmsMemberService memberService;

    private static final Logger LOGGER = LoggerFactory.getLogger(UmsMemberController.class);



    @ApiOperation("会员注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult register(@RequestBody UmsLoginOrRegisterParam lr) {

        Map<String, String> tokenMap  =  memberService.register(lr.getUsername(),
                                                            lr.getPassword(),
                                                            lr.getTelephone(),
                                                            lr.getAuthCode());


            return CommonResult.success(tokenMap);
    }

    @ApiOperation("账号密码登录")
    @RequestMapping(value = "/pwdLogin", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult pwdLogin(@RequestBody UmsLoginOrRegisterParam lr) {
        Map<String, String> tokenMap  =memberService.pwdLogin(lr.getUsername(), lr.getPassword());

        return CommonResult.success(tokenMap);
    }

    @ApiOperation("手机验证码登录")
    @RequestMapping(value = "/smsLogin", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult smsLogin(@RequestBody UmsLoginOrRegisterParam lr) {

        Map<String, String> tokenMap = memberService.smsLogin(lr.getTelephone(), lr.getAuthCode());

        return CommonResult.success(tokenMap);
    }

    @ApiOperation("获取验证码")
    @RequestMapping(value = "/getAuthCode", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getAuthCode(@RequestBody UmsLoginOrRegisterParam lr) {
        return CommonResult.success(memberService.generateAuthCode(lr.getTelephone()));
    }


    @ApiOperation("修改密码")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updatePassword(@RequestBody UmsLoginOrRegisterParam lr) {
        return CommonResult.success(memberService.updatePassword(lr.getTelephone(),lr.getPassword(),lr.getAuthCode()));
    }

    @ApiOperation(value = "根据username获得用户")
    @RequestMapping(value = "/getByUsername", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<MemberDetails> loadUserByUsername(@RequestParam("username") String username) {
        MemberDetails umsUmsDetails = memberService.loadUserByUsername(username);
        return  CommonResult.success(umsUmsDetails);
    }

    @ApiOperation(value = "根据userId获得用户")
    @RequestMapping(value = "/getByUserId/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMember> getByUserId(@PathVariable("userId") Long userId) {
        UmsMember umsMember = memberService.getById(userId);
        return  CommonResult.success(umsMember);
    }



    @ApiOperation(value = "当前登录用户")
    @RequestMapping(value = "/getCurrentUser", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsMember> getCurrentUser( ) {
        UmsMember umsMember = memberService.getCurrentMember();
        return  CommonResult.success(umsMember);
    }

    @ApiOperation(value = "updateIntegration")
    @RequestMapping(value = "/updateIntegration", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateIntegration(@RequestParam("userId") Long userId, @RequestParam("integration") Integer integration) {
        int result =  memberService.updateIntegration(userId, integration);
            return  CommonResult.success(result);
    }

}